<?php
    /*
     * 1.服务器需要开启curl
     * 2.服务器需要放system命令
     * 3.服务器类别自己定义
     * 4.将urlinfo导入数据库中，修改数据库信息
     * 5.nginx需要新增配置如下,用于自动跳转,可参考nginx.conf文件
     * #rewrite
        server 
        {
         listen     80;
         server_name  aaa.xxx.com;
         rewrite  "^/(.*)$"  http://aaa.xxx.com/$1 break;
        }  
     */
   ignore_user_abort(); // 后台运行
   set_time_limit(0); // 取消脚本运行时间的超时上限
   $nginx = '/usr/local/nginx/sbin/nginx';//nginx 机器安装位置
   $config = '/usr/local/nginx/conf/nginx.conf';//nginx 
   $application_config = '/mydata/ly/www/application/config/config.php';
   $const_config = '/mydata/ly/www/application/libraries/Game_CONST.php';
   $prefix = 'http://';
   $common_select_sql = "select * from urlinfo where ";
   $type = 'B';
   $common_type_sql = " and type = '".$type."'";
   $host = 'rm-wz97m7f230mzxryh5.mysql.rds.aliyuncs.com';//数据库地址
   $username = 'proadmin';//数据库帐号
   $pwd = 'Xiuxian2018';//数据库密码
   $database = 'xiuxian';//数据库名称
   while(!file_exists('close')){
    
        $connect=mysqli_connect($host,$username,$pwd,$database);
        if (mysqli_connect_errno()) {
            exit();
        }
        
        $sql=$common_select_sql."status = 0".$common_type_sql;//服务器类别自己定义
        
        $result=mysqli_query($connect,$sql);

        if (!$result) {
            continue;
        }

        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        //1.获取当前使用域名
        $old_url = $row['domain'];

        // 2.调用api检测 --根据实际的购买api进行更改
        $url='http://www.xxweixin.com/weixin/jc403585363.php?domain='.$old_url;//api地址
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        $file_contents = json_decode(curl_exec($ch),true);
        curl_close($ch);
        //echo (int)$file_contents['status'].$old_url;
        //3.当域名被封时操作
        if((int)$file_contents['status'] != 0){
            //4.获取可用新地址
            $sql=$common_select_sql."status = 1 and deleted != 1".$common_type_sql;
            $result=mysqli_query($connect,$sql);

            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

            $new_url = $row['domain'];
            if(empty($new_url)){
               //短信通知

               continue;
            }
            //5.删除旧地址
            $sql = "update urlinfo set deleted = 1 , status = 1 where domain = '".$old_url."'".$common_type_sql;
            mysqli_query($connect,$sql);
            $sql = "update urlinfo set status = 0 where domain = '".$new_url."'".$common_type_sql;
            mysqli_query($connect,$sql);
            //6.读取nginx配置文件
            $data = file_get_contents($config);
            //7.修改配置文件
            $data_new = str_replace($prefix.$old_url, $prefix.$new_url, $data);
            $fp = fopen($config, 'r+');
            fwrite($fp, $data_new);
            fclose($fp);
            //8.读取应用config文件
            if($type == 'B'){
                $app_data = file_get_contents($application_config);
                $data_new = str_replace($prefix.$old_url, $prefix.$new_url, $app_data);
                $fp = fopen($application_config, 'r+');
                fwrite($fp, $data_new);
                fclose($fp);
                $app_data = file_get_contents($const_config);
                $data_new = str_replace($prefix.$old_url, $prefix.$new_url, $app_data);
                $fp = fopen($const_config, 'r+');
                fwrite($fp, $data_new);
                fclose($fp);
            }
            //9.重启
            // echo system("killall -9 nginx");
            // sleep(1);
            // echo system("ulimit -SHn 51200");
            system($nginx." -s reload");
        }
        
        sleep(30);

        mysqli_close($connect);
   }
   exit();
?>